<?php
    /**
    * Author:rui
    * Email:zer0131@vip.qq.com
    * 商品展示
    */
    class IndexAction extends BaseAction{

        private $goodsobj;
        public function _initialize(){
            parent::_initialize();
            $this->goodsobj = D('Goods');
            $SEO = seo("", "", AppframeAction::$Cache['Config']['siteinfo'], AppframeAction::$Cache['Config']['sitekeywords']);
            $this->assign("SEO", $SEO);
        }
		//首页
		public function index() {
			$this->display();
		}
        //商品详细页
    	public function detail(){
            $id = $this->_get('id');
            if(!$id){
                $this->error('参数错误');
            }
            $map['id'] = $id;
            $data = $this->goodsobj->where($map)->find();
            if($data){
                if($data['category'] == '1'){
                    $data['cname'] = '买新房';
                }
                else if($data['category'] == '2'){
                    $data['cname'] = '二手房租售';
                }
                $data['discount'] = round($data['nowprice']/$data['oldprice']*10);
                $data['spread'] = $data['oldprice'] - $data['nowprice'];
                $data['nowprice'] = intval($data['nowprice']);
                $data['oldprice'] = intval($data['oldprice']);
                $purchased = M('Order')->where(array('goodsid'=>$id))->count();
                $data['purchased'] = $purchased + $data['hasbuy'];
            }
            else{
                $this->error('该项不存在');
            }
            $this->assign('goods',$data);
            $this->display();
           
    	}

    	//商品列表
    	public function listGoods(){
    		$cid = $this->_get('cid');
            if(!$cid){
                $this->error('参数错误');
            }
            $map['category'] = $cid;
            $map['endtime']=array("gt",time());

            $count = $this->goodsobj->where($map)->count();
            $page = $this->page($count, 9);

            $list = $this->goodsobj->field('id,pic,title,nowprice,attention,starttime,endtime,goodsurl,category')->where($map)->limit($page->firstRow . ',' . $page->listRows)->order('addtime desc')->select();
            foreach ($list as $key => $value) {
                $list[$key]['nowprice'] = intval($value['nowprice']);
            }
            if($cid == '1'){
                $cname = '买新房';
            }
            else if($cid == '2'){
                $cname = '二手房租售';
            }
            $this->assign('cid', $cid);
            $this->assign('list',$list);
            $this->assign('cname', $cname);
            $this->assign("page", $page->show());
            $this->display();
    	}

    	//购物车
    	public function cart(){
            $id = $this->_get('id');
            if(!$id){
                $this->error('参数有误');
            }
            $map['id'] = $id;
            $data = $this->goodsobj->where($map)->find();
            if($data){
                if($data['endtime'] < time()){
                    $this->error('活动已结束',U('Buy/Index/index',array('id'=>$id)));
                }
                $this->assign('data', $data);
            }
            else{
                $this->error('该项不存在');
            }
    		$this->display();
    	}

        //结账
        public function checkout(){
            $id = $this->_get('flag');
            if(!$id){
                $this->error('参处错误');
            }
            $data = M('Order')->where(array('id'=>$id))->find();
            if($data['ispay'] == '0'){
                $goods = $this->goodsobj->field('id,title,nowprice,oldprice,starttime,endtime')->where(array('id'=>$data['goodsid']))->find();
                $this->assign('order',$data);
                $this->assign('goods', $goods);
                $this->display();
            }
            else{
                $this->error('已付款',U('Buy/Goods/index',array('id'=>$data['goodsid'])));
            }
            
        }

        //生成订单号
        protected function getOrderSn(){
            return date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        }

        //生成订单
        public function createOrder(){
            $orderobj = D('Order');
            if(!$orderobj->create()){
                $this->error($orderobj->getError());
            }
            $orderobj->ordersn = $this->getOrderSn();
            $orderobj->ispay = 0;
            $result = $orderobj->add();
            if($result){
                header('Content-Type:text/html;charset=utf-8');
                $this->redirect('Buy/Index/checkout', array('flag'=>$result),3, '数据提交中...');
            }
            else{
                $this->error('提交订单失败');
            }
        }
    }
?>